// 这里是三级分类的仓库
import {
  reqCategory1List,
  reqCategory2List,
  reqCategory3List,
} from "@/api/category";

export default {
  namespaced: true,
  state: {
    category1List: [],
    category2List: [],
    category3List: [],
    category1Id: "",
    category2Id: "",
    category3Id: "",
  },
  mutations: {
    UPDATE_1LIST(state, data) {
      state.category1List = data;
    },
    UPDATE_2LIST(state, { data, id }) {
      state.category2List = data;
      state.category1Id = id;
      state.category2Id = "";
      state.category3Id = "";
      state.category3List = [];
    },
    UPDATE_3LIST(state, { data, id }) {
      state.category3List = data;
      state.category2Id = id;
      state.category3Id = "";
    },
    UPDATE_3ID(state, id) {
      state.category3Id = id;
    },
  },
  actions: {
    async getCategory1List({ commit }) {
      try {
        const res = await reqCategory1List();
        commit("UPDATE_1LIST", res.data);
      } catch (error) {
        console.log(error);
      }
    },

    async getCategory2List({ commit }, id) {
      try {
        const res = await reqCategory2List(id);
        commit("UPDATE_2LIST", { data: res.data, id });
      } catch (error) {
        console.log(error);
      }
    },

    async getCategory3List({ commit }, id) {
      try {
        const res = await reqCategory3List(id);
        commit("UPDATE_3LIST", { data: res.data, id });
      } catch (error) {
        console.log(error);
      }
    },
  },
  getters: {},
};
